/**
 * @Copyright：<a href="http://www.wisesoft.net.cn">四川川大智胜系统集成有限公司</a>
 * @Author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
 * @CreateTime: 2022年08月06日  17:42
 */
package com.fxsen.commons.utils;

import org.apache.commons.codec.binary.Base64;

import java.io.*;

/**
 * 使用base64编码方式对文件和字符串进行编码解码
 *@Author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
 *@CreateTime: 2022年08月06日  17:42
 */
public class Base64Utils {
    /**
     * 编码字节数组
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/6 17:42
 	 * @param: bt
 	 * @return: {@link byte[]}
    **/
    public static byte[] encodeByte(byte[] bt) {
        Base64 base64 = new Base64();
        return base64.encode(bt);
    }

    /**
     * 解码字节数组
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/6 17:43
 	 * @param: bt
 	 * @return: {@link byte[]}
    **/
    public static byte[] decodeByte(byte[] bt){
        Base64 base64 = new Base64();
        return base64.decode(bt);
    }

   /**
    * 加密字符串
    * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
    * @date: 2022/8/6 17:43
 	* @param: str
    * @param: charSet
    * @return: {@link String}
   **/
    public static String encodeStr(String str,String charSet) throws UnsupportedEncodingException{
        //将字符串转化为指定字符集的字节序列
        byte[] bt = str.getBytes(charSet);
        return new String(encodeByte(bt),charSet);
    }

    /**
     * 解密字符串
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/6 17:43
 	 * @param: str
 	 * @param: charSet
 	 * @return: {@link String}
    **/
    public static String decodeStr(String str,String charSet) throws UnsupportedEncodingException{
        //将字符串转化为指定字符集的字节序列
        byte[] bt = str.getBytes(charSet);
        return new String(decodeByte(bt),charSet);
    }

    /**
     * 加密指定文件
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/6 17:43
 	 * @param: file
 	 * @return: {@link byte[]}
    **/
    public static byte[] encodeFileToByte(File file) throws IOException{
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
        byte[] bt = new byte[in.available()];
        in.read(bt);
        in.close();
        return encodeByte(bt);
    }

    /**
     * 加密指定文件并返回指定编码的字符串
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/6 17:43
 	 * @param: file
 	 * @param: charSet
 	 * @return: {@link String}
    **/
    public static String encodeFileToStr(File file,String charSet) throws IOException{
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
        byte[] bt = new byte[in.available()];
        in.read(bt);
        in.close();
        return new String(encodeByte(bt),charSet);
    }
    /**
     * 解密指定文件对象
     * @author: <a href="mailto:fxsen@foxmail.com">Fxsen</a>
     * @date: 2022/8/6 17:43
 	 * @param: file
 	 * @param: bt
 	 * @return: {@link File}
    **/
    public static File decodeByteToFile(File file, byte[] bt) throws IOException{
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
        out.write(decodeByte(bt));
        out.flush();
        out.close();
        return file;
    }
}
